import java.util.Timer;
import java.util.TimerTask;

public class ControlPanel {
    private static final int PASSWORD_LENGTH = 4;
    private static final int MAX_ATTEMPTS = 3;
    private static final int LOCK_TIME = 120000; // 120秒

    private String password;
    private String inputBuffer = "";
    private int attemptCount = 0;
    private boolean isLocked = false;
    private Timer lockTimer;


    public ControlPanel(String password) {
        this.password = password;
    }

    public void keyPressed(char key) {
        if (isLocked) {
            System.out.println("控制面板锁定，请等待！");
            return;
        }

        inputBuffer += key;
        if (inputBuffer.length() == PASSWORD_LENGTH) {
            verifyPassword();
        } else {
            System.out.println("Reading input: " + inputBuffer);
        }
    }

    // 验证密码
    private void verifyPassword() {
        if (inputBuffer.equals(password)) {
            System.out.println("密码正确！进入系统功能选择");
            enterFunctionalState();
        } else {
            attemptCount++;
            if (attemptCount >= MAX_ATTEMPTS) {
                lockPanel();
            } else {
                inputBuffer = "";
                System.out.println("密码错误，请重试！");
            }
        }
    }

    // 功能选择
    private void enterFunctionalState() {
        System.out.println("系统功能选择");
    }

    // 锁面板
    private void lockPanel() {
        System.out.println("锁定控制面板");
        isLocked = true;
        startLockTimer();
    }

    // Start the timer for unlocking the panel
    private void startLockTimer() {
        lockTimer = new Timer();
        lockTimer.schedule(new TimerTask() {
            @Override
            public void run() {
                unlockPanel();
            }
        }, LOCK_TIME);
    }

    // Unlock the panel
    private void unlockPanel() {
        System.out.println("控制面板将在120秒后解锁");
        isLocked = false;
        attemptCount = 0;
        inputBuffer = "";
    }

    public static void main(String[] args) {
        ControlPanel panel = new ControlPanel("1234");

        // Simulate key presses
        panel.keyPressed('1');
        panel.keyPressed('2');
        panel.keyPressed('3');
        panel.keyPressed('5'); // 错误密码
        panel.keyPressed('1');
        panel.keyPressed('2');
        panel.keyPressed('3');
        panel.keyPressed('4'); // 正确密码
    }
}
